package cn.daniellee.plugin.vs.command;

import cn.daniellee.plugin.vs.Visitors;
import cn.daniellee.plugin.vs.core.VisitorCore;
import cn.daniellee.plugin.vs.runnable.VisitorsRunnable;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.chat.ClickEvent;
import net.md_5.bungee.api.chat.ComponentBuilder;
import net.md_5.bungee.api.chat.HoverEvent;
import net.md_5.bungee.api.chat.TextComponent;
import org.bukkit.Location;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import java.util.LinkedList;

public class VisitorsCommand implements CommandExecutor {

    @Override
    public boolean onCommand(CommandSender commandSender, Command command, String s, String[] strings) {
        if (strings.length > 0) {
            if (commandSender.isOp()) {
                if ("reload".equalsIgnoreCase(strings[0])) {
                    Visitors.getInstance().reloadConfig();
                    Visitors.getInstance().loadConfig();
                    commandSender.sendMessage((Visitors.getInstance().getPrefix() + Visitors.getInstance().getConfig().getString("message.reload-success", "&eConfiguration reload completed.")).replace("&", "§"));
                    return true;
                } else if ("begin".equalsIgnoreCase(strings[0])) {
                    if (!(commandSender instanceof Player)) {
                        if (!VisitorCore.recording.containsKey(commandSender.getName())) {
                            if (!VisitorCore.cached.containsKey(commandSender.getName())) {
                                VisitorCore.recording.put(commandSender.getName(), new LinkedList<>());
                                commandSender.sendMessage((Visitors.getInstance().getPrefix() + Visitors.getInstance().getConfig().getString("message.record-begin", "&eRecording has started, please start moving.")).replace("&", "§"));
                            } else commandSender.sendMessage((Visitors.getInstance().getPrefix() + Visitors.getInstance().getConfig().getString("message.exist-cache", "&eYou have unfinished recordings, please complete the configuration.")).replace("&", "§"));
                        } else commandSender.sendMessage((Visitors.getInstance().getPrefix() + Visitors.getInstance().getConfig().getString("message.already-begin", "&eYou have started recording, &b/vr end &eto end recording.")).replace("&", "§"));
                    } else commandSender.sendMessage((Visitors.getInstance().getPrefix() + Visitors.getInstance().getConfig().getString("message.only-player", "&eThe console cannot execute this command.")).replace("&", "§"));
                } else if ("end".equalsIgnoreCase(strings[0])) {
                    if (!(commandSender instanceof Player)) {
                        if (VisitorCore.recording.containsKey(commandSender.getName())) {



                            LinkedList<Location> record = VisitorCore.recording.get(commandSender.getName());
                            VisitorCore.cached.put(commandSender.getName(), record);
                            VisitorCore.recording.remove(commandSender.getName());
//                            VisitorsRunnable.visitors.put(commandSender.getName(), record);

                            commandSender.sendMessage((Visitors.getInstance().getPrefix() + Visitors.getInstance().getConfig().getString("message.record-end", "&eRecording is over, please configure according to the menu.")).replace("&", "§"));
                        } else commandSender.sendMessage((Visitors.getInstance().getPrefix() + Visitors.getInstance().getConfig().getString("message.not-begin", "&eRecording has not yet started, &b/vr begin &eto start recording.")).replace("&", "§"));
                    } else commandSender.sendMessage((Visitors.getInstance().getPrefix() + Visitors.getInstance().getConfig().getString("message.only-player", "&eThe console cannot execute this command.")).replace("&", "§"));
                } else if ("config".equalsIgnoreCase(strings[0])) {
                    if (!(commandSender instanceof Player)) {
                        if (VisitorCore.cached.containsKey(commandSender.getName())) {

                            commandSender.sendMessage((Visitors.getInstance().getPrefix() + Visitors.getInstance().getConfig().getString("message.record-end", "&eRecording is over, please configure according to the menu.")).replace("&", "§"));
                        } else commandSender.sendMessage((Visitors.getInstance().getPrefix() + Visitors.getInstance().getConfig().getString("message.not-begin", "&eRecording has not yet started, &b/vr begin &eto start recording.")).replace("&", "§"));
                    } else commandSender.sendMessage((Visitors.getInstance().getPrefix() + Visitors.getInstance().getConfig().getString("message.only-player", "&eThe console cannot execute this command.")).replace("&", "§"));
                }
            }
        } else sendHelp(commandSender);
        return true;
    }


    private void sendHelp(CommandSender commandSender) {
        commandSender.sendMessage(("&m&a----&m&6----------&3 " + Visitors.getInstance().getConfig().getString("prompt-prefix", "Visitors") + " &m&6----------&m&a----").replace("&", "§"));

        String targetText = Visitors.getInstance().getConfig().getString("help.target", "Set the target player as the broadcast object.").replace("&", "§");
        TextComponent targetHelp = new TextComponent("/vs target  " + targetText);
        targetHelp.setColor(ChatColor.GRAY);
        targetHelp.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/vs target "));
        targetHelp.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(targetText).color(ChatColor.BLUE).create()));
        commandSender.spigot().sendMessage(targetHelp);

        String timeText = Visitors.getInstance().getConfig().getString("help.time", "Set the time each player is broadcast(unit: second).").replace("&", "§");
        TextComponent timeHelp = new TextComponent("/vs time  " + timeText);
        timeHelp.setColor(ChatColor.GRAY);
        timeHelp.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/vs time "));
        timeHelp.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(timeText).color(ChatColor.BLUE).create()));
        commandSender.spigot().sendMessage(timeHelp);

        String recorderText = Visitors.getInstance().getConfig().getString("help.recorder", "Set the target player as the live recorder.").replace("&", "§");
        TextComponent recorderHelp = new TextComponent("/vs recorder  " + recorderText);
        recorderHelp.setColor(ChatColor.GRAY);
        recorderHelp.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/vs recorder "));
        recorderHelp.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(recorderText).color(ChatColor.BLUE).create()));
        commandSender.spigot().sendMessage(recorderHelp);

        String resetText = Visitors.getInstance().getConfig().getString("help.reset", "Resetting the live broadcast time will automatically switch the live broadcast target.").replace("&", "§");
        TextComponent resetHelp = new TextComponent("/vs reset  " + resetText);
        resetHelp.setColor(ChatColor.GRAY);
        resetHelp.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/vs reset"));
        resetHelp.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(resetText).color(ChatColor.BLUE).create()));
        commandSender.spigot().sendMessage(resetHelp);

        String toggleText = Visitors.getInstance().getConfig().getString("help.toggle", "&eSet whether you will be broadcast live (for players).").replace("&", "§");
        TextComponent toggleHelp = new TextComponent("/vs toggle  " + toggleText);
        toggleHelp.setColor(ChatColor.GRAY);
        toggleHelp.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/vs toggle"));
        toggleHelp.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(resetText).color(ChatColor.BLUE).create()));
        commandSender.spigot().sendMessage(toggleHelp);

        String reloadText = Visitors.getInstance().getConfig().getString("help.reload", "Reload configuration.").replace("&", "§");
        TextComponent reloadHelp = new TextComponent("/vs reload  " + reloadText);
        reloadHelp.setColor(ChatColor.GRAY);
        reloadHelp.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/vs reload"));
        reloadHelp.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(reloadText).color(ChatColor.BLUE).create()));
        commandSender.spigot().sendMessage(reloadHelp);
    }
}
